package com.xtsoft.platform.base.system.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.xtsoft.core.controller.GenericController;
import com.xtsoft.core.repository.QueryFilter;
import com.xtsoft.core.repository.QueryResult;
import com.xtsoft.core.utils.BeanUtil;
import com.xtsoft.core.utils.ResultMsg;
import com.xtsoft.core.utils.StringUtils;
import com.xtsoft.platform.base.system.domain.SysDashboard;
import com.xtsoft.platform.base.system.service.SysDashboardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.util.List;


 /**
 * 
 * @ClassName:  SysDashboard   
 * <p> 仪表盘处理类 </p>
 * @author: Codegen-代码生成器
 * @date:   2019年12月3日 上午10:59:46
 */ 
@Tag(name = "仪表盘--处理接口")
@RestController
public class SysDashboardController extends GenericController{
	
	private final Logger log = LoggerFactory.getLogger(SysDashboardController.class);
	
	@Autowired	
	private SysDashboardService sysDashboardService;
	
	
    /**
     *  <p>新增一个仪表盘</p>  
     * @param sysDashboard 要新增的仪表盘
     * @return ResponseEntity<ResultMsg>, ResultMsg的Data中包含 仪表盘
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常                     
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38
    */
	@Operation(summary="新增，根据前台传递的仪表盘新增")
	@Parameter(name = "sysDashboard",description = "要新增的仪表盘")
    @PostMapping("/sysDashboard/add")
    public ResponseEntity<ResultMsg> add(@RequestBody SysDashboard sysDashboard) throws URISyntaxException {
		if(null == sysDashboard){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增仪表盘失败，传递的仪表盘为空"),HttpStatus.OK); 
		}
		log.info("新增仪表盘开始......");
        if (!StringUtils.isEmpty(sysDashboard.getDashboardId())) {
		    log.info("新增仪表盘失败，新仪表盘不能具有DashboardId属性值");
            return new ResponseEntity<ResultMsg>(new ResultMsg(false,"新增仪表盘失败，新仪表盘不能具有DashboardId属性值"),HttpStatus.OK);
        }
		
        SysDashboard result = sysDashboardService.save(sysDashboard);
		log.info("新增仪表盘成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"新增成功",result),HttpStatus.OK);
    }	

    /**
     * <p>更新一个仪表盘</p>    
     * @param sysDashboard 要更新的仪表盘
     * @return ResponseEntity<ResultMsg>, json格式
     * @throws URISyntaxException 如果URI语法不正确，则抛URI语法异常
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38
     */
	@Operation(summary ="更新，根据传递的 仪表盘更新实体")
	@Parameter(name = "sysDashboard", description = "要更新的仪表盘")
    @PostMapping("/sysDashboard/update")
    public ResponseEntity<ResultMsg> update(@RequestBody SysDashboard sysDashboard) throws URISyntaxException, IllegalAccessException, InvocationTargetException {
		if(null == sysDashboard){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新仪表盘失败，传递的仪表盘为空"),HttpStatus.OK); 
		}
		log.info("单条更新仪表盘开始......");
		//若 仪表盘无主键值，说明调用错误
        if (StringUtils.isBlank(sysDashboard.getDashboardId())) {
			log.info("单条更新仪表盘失败，仪表盘对象DashboardId无属性值");
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"更新失败，仪表盘对象DashboardId无属性值"),HttpStatus.OK);
        }
        //若类存在存在关联关系，前台传递过来的关联属性大概率为空，执行以下处理
		SysDashboard result = sysDashboardService.get(sysDashboard.getDashboardId());
		BeanUtil.copyNotNullProperties(result, sysDashboard);
		result = sysDashboardService.save(result);
		log.info("单条更新仪表盘成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"更新成功",result),HttpStatus.OK);
    }  

    /**
	 *   <p>查询仪表盘列表，若分页，则页码信息在Request的参数中，包括当前页，每页记录数</p>
     * @return ResponseEntity<QueryResult<SysDashboard>>，json格式
     * @throws URISyntaxException 如果出错，则产生页码信息的 HTTP headers
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38
     */
	@Operation(summary="查询，根据传递的条件查询仪表盘列表，条件参数在Request中")
    @GetMapping("/sysDashboard/getAll")
    public ResponseEntity<ResultMsg> getAll()
        throws URISyntaxException, JsonParseException, JsonMappingException, IOException, ParseException {
		log.info("获取仪表盘列表开始......");
        QueryFilter<SysDashboard> filter=new QueryFilter<SysDashboard>(SysDashboard.class);
        filter.addFilterFromRequest(request);
        QueryResult<SysDashboard> result= sysDashboardService.anyQuery(filter);
		if(null == result || null == result.getResultList() || result.getResultList().size() < 0){
			return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取仪表盘列表失败"),HttpStatus.OK);
		}
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"获取列表成功",result.getResultList()),HttpStatus.OK);
    }

    /**
	 *   <p>根据仪表盘唯一ID值获取仪表盘</p>
     * @param id 仪表盘唯一ID值
     * @return  ResponseEntity<SysDashboard>
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38		
     */
	@Parameter(name = "id", description="对象唯一Id", required = false)
	@Operation(summary ="获取单个仪表盘，若根据仪表盘唯一Id获取到了多个对象则发生异常")
    @GetMapping("/sysDashboard/get/{id}")
    
	
    public ResponseEntity<ResultMsg> get(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个仪表盘失败，未给定仪表盘的唯一标识"),HttpStatus.OK);
		}
    	log.info("获取单条仪表盘开始......");
    	SysDashboard sysDashboard = sysDashboardService.get(id);
    	if(sysDashboard==null) {
    		return new ResponseEntity<ResultMsg>(new ResultMsg(false,"获取单个仪表盘失败，仪表盘为空"),HttpStatus.OK);
    	}
    	log.info("获取单条仪表盘成功......");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true, "获取成功", sysDashboard), HttpStatus.OK);
    } 

    /**
     *     <p>根据仪表盘唯一ID值删除单个仪表盘</p>
     * @param  id 仪表盘唯一ID值
     * @return ResponseEntity,json格式
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38		 
     */
	@Parameter(name = "id", description="仪表盘唯一Id", required = false)
	@Operation(summary ="删除单个仪表盘") 
	@GetMapping("/sysDashboard/delete/{id}")
	
    public ResponseEntity<ResultMsg> delete(@PathVariable String id) {
		if(StringUtils.isBlank(id)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"删除单个仪表盘失败，未给定仪表盘的唯一标识"),HttpStatus.OK);
		}
		log.info("删除单条仪表盘开始......");
		sysDashboardService.remove(id);
		log.info("删除单条仪表盘成功");		
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"删除成功"),HttpStatus.OK);
    }
	
    /**
     *     <p>根据仪表盘唯一ID值集合，批量删除仪表盘</p>
     * @param  ids 仪表盘唯一ID值集合
     * @return ResponseEntity<ResultMsg>,返回结果中有删除的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38		 
     */
	@Parameter(name = "ids", description="逗号分隔的仪表盘Id集合", required = false)
	@Operation(summary ="删除仪表盘集合") 
    @PostMapping("/sysDashboard/deleteBatch")
	
    public ResponseEntity<ResultMsg> deleteBatch(@RequestParam String ids) {
		if(StringUtils.isBlank(ids)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"批量删除仪表盘失败，未给定仪表盘的唯一标识集合"),HttpStatus.OK);
		}		
		log.info("批量删除仪表盘开始......");
        long rows = sysDashboardService.deleteBatch(ids);
		log.info("批量删除仪表盘成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"批量删除仪表盘成功",rows),HttpStatus.OK);
    }
	
    /**
     *     <p>根据仪表盘唯一ID值集合，批量更新仪表盘</p>
     * @param  ids 仪表盘唯一ID值集合
	 * @param  properityName 仪表盘要更新的属性名，不带类名
	 * @param  properityValue 仪表盘要更新的属性值
     * @return ResponseEntity<ResultMessage>,返回结果中有更新的记录数，json格式
	 * @author Codegen-代码生成器
	 * @date 2021-04-27 10:16:38		 
     */
	
	
	@Parameters({
	@Parameter(name = "properityName", description="要更新的属性名不带类名，只能是类单个属性名且不能是集合类型", required = false),
	@Parameter(name = "properityValue", description="属性值", required = false),
	@Parameter(name = "ids", description="逗号分隔的仪表盘Id集合", required = false)})
	@Operation(summary ="更新仪表盘集合") 
    @PostMapping("/sysDashboard/updateBatch")
 
    public ResponseEntity<ResultMsg> updateBatch(@RequestParam String properityName,
		@RequestParam String properityValue,
		@RequestParam String ids) {
		if(StringUtils.isBlank(ids) || StringUtils.isBlank(properityValue) || StringUtils.isBlank(properityName)){
			 return new ResponseEntity<ResultMsg>(new ResultMsg(false,"批量更新仪表盘失败，未给定仪表盘的唯一标识集合或属性名或属性值"),HttpStatus.OK);
		}		
		log.info("批量更新仪表盘开始......");
        long rows = sysDashboardService.updateBatch(properityName,properityValue,ids);
		log.info("批量更新仪表盘成功");
        return new ResponseEntity<ResultMsg>(new ResultMsg(true,"批量更新仪表盘成功",rows),HttpStatus.OK);
    }	
    
	@Parameter(name = "roleId", description="角色Id", required = false)
	@Operation(summary ="根据角色Id获取面板树") 
    @GetMapping("/sysDashboard/getDashboardTree")
    public ResponseEntity<String> getDashboardTree(@RequestParam("roleId")String roleId){
		return new ResponseEntity<>(sysDashboardService.getDashboardTree(roleId),HttpStatus.OK);
    }
    
    
	@Parameter(name = "rolealias", description="角色别名", required = false)
	@Operation(summary ="根据角色别名获取角色") 
    @GetMapping("/sysDashboard/findByRole/{rolealias}")
    public ResponseEntity<List<SysDashboard>> bulidTemplate(@PathVariable String rolealias) {
    	return new ResponseEntity<>(sysDashboardService.findByRole(rolealias), HttpStatus.OK);
    }
    
    
}
